'use strict';

const Controller = require('egg').Controller;
/**
 * @Controller 用户登录注册模块
 */
class UserController extends Controller {
  /**
     * @summary 登录
     * @description 登录
     * @router post /user/login
     * @request body loginParams *body
     * 
   */
  async login() {
    const { ctx, service, app } = this;
    let { account, password, code } = ctx.request.body;
    ctx.validate({
      account: 'string',
      password: 'string',
      code: 'string'
    }, ctx.request.body);
    console.log(ctx.session.code, code, '---')
    if (ctx.session.code.toLowerCase() != code.toLowerCase()) {

      return ctx.body = {
        status: 1,
        message: '验证码错误'
      };
    }
    const selectAccount = await service.user.selectAccount(account);

    if (selectAccount == 'err') {
      return ctx.body = {
        status: 1,
        message: '系统发生错误，请稍后再试'
      };
    }
    if (selectAccount) {
      // 校验密码
      const checkPassword = this.ctx.helper.compare(
        password,
        selectAccount.password,
      );
      if (!checkPassword) {
        return ctx.body = {
          status: 1,
          message: '登录失败'
        };
      }
      if (selectAccount.status) {
        return ctx.body = {
          status: 1,
          message: '账户冻结'
        };
      }
      let data = { ...selectAccount, password: "", imageUrl: "", create_time: "", update_time: "" }
      // sign 的data 可以是对象
      const token = app.jwt.sign(data, app.config.jwt.secret);
      ctx.body = {
        status: 0,
        message: '登录成功',
        token: 'Bearer ' + token,
        data: { ...selectAccount, password: "" }
      };
    } else {
      ctx.body = {
        status: 1,
        message: '用户名或密码错误'
      };
    }
  }

  /**
 * @summary 登录的验证码
 * @description 登录的验证码
 * @router get /user/getCaptcha
 * 
*/
  async getCaptcha() {
    const { ctx, service, app } = this;
    // service方法
    let captcha = await this.service.user.captcha();
    // 返回的类型格式
    ctx.response.type = 'image/svg+xml';
    ctx.body = {
      img: captcha.data,
      status: 0
    }
  }

  /**
     * @summary 忘记密码的验证
     * @description 忘记密码的验证
     * @router post /user/verify
     * @request body verifyParams *body
     * 
   */
  async verify() {
    const { ctx, service, app } = this;
    let data = {
      account: ctx.request.body.account,
      email: ctx.request.body.email
    }
    ctx.validate({
      account: 'string',
      email: 'string'
    }, ctx.request.body);
    // service方法
    let result = await this.service.user.verify(data);
    if (result == 'err') {
      return ctx.body = {
        status: 1,
        message: '系统发生错误，请稍后再试'
      };
    }
    if (result) {
      ctx.body = {
        status: 0,
        message: '验证成功',
        id: result.id
      };
    } else {
      ctx.body = {
        status: 1,
        message: '验证失败'
      };
    }
  }
  /**
     * @summary 重置密码
     * @description 重置密码
     * @router post /user/resetPassword
     * @request body resetPasswordParams *body
     * 
   */
  async resetPassword() {
    const { ctx, service, app } = this;
    const { password, id, } = ctx.request.body;
    let data = {
      password: this.ctx.helper.encrypt(password),
      id: id
    }

    let result = await service.user.resetPassword(data);
    if (result == 'err') {
      return ctx.body = {
        status: 1,
        message: '系统发生错误，请稍后再试'
      };
    }
    console.log(result)
    if (result.affectedRows == 1) {
      ctx.body = {
        status: 0,
        message: '修改成功',
        id: result.id
      };
    } else {
      ctx.body = {
        status: 1,
        message: '修改失败'
      };
    }




  }




}

module.exports = UserController;
